
package com.android.fitnesscenter.utils;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;

public class ActivityHelper {
    /**
     * Get screen width
     * 
     * @param activity
     * @return
     */
    @SuppressWarnings("deprecation")
    public static int getScreenWidth(Activity activity) {
        Display display = activity.getWindow().getWindowManager().getDefaultDisplay();
        return display.getWidth();
    }

    /**
     * Get screen height
     * 
     * @param activity
     * @return
     */
    @SuppressWarnings("deprecation")
    public static int getScreenHeight(Activity activity) {
        Display display = activity.getWindow().getWindowManager().getDefaultDisplay();
        return display.getHeight();
    }

    /**
     * Get screen density
     * 
     * @param activity
     * @return
     */
    public static float getScreenDensity(Activity activity) {
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindow().getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.density;
    }

    /**
     * get screen scale
     * 
     * @param activity
     * @return
     */
    public static float getScreenScale(Activity activity) {
        return getScreenDensity(activity) / 160;
    }

    /**
     * dip to px ：计算公式 pixels = dips * (density / 160)
     * 
     * @param density
     * @param dipValue
     * @return
     */
    public static int dip2px(float scale, float dipValue) {
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * Px to dip
     * 
     * @param density
     * @param pxValue
     * @return
     */
    public static int px2dip(float scale, float pxValue) {
        return (int) (pxValue / scale + 0.5f);
    }

}
